package Comparable01;


import java.util.Arrays;

class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
//按年龄排序
//    @Override
//    public int compareTo(Student o) {
//        return this.age-o.age;
//    }

//按名字排序
    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
    }
}
public class Test {
    //按年龄排序
    public static void main1(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",18);
        students[1] = new Student("lisi",39);
        students[2] = new Student("wangwu",26);
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }
    //按名字排序
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",18);
        students[1] = new Student("lisi",39);
        students[2] = new Student("wangwu",26);
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }
}

